﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using RS.BaseModel.Common;
using RS.Tool.Init;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace BaseAuthRole.AspNetCore.Filters
{
    public class ApiAuthCheckFilter : ActionFilterAttribute
    {
        //action执行后
        public override void OnActionExecuted(ActionExecutedContext context)
        {

        }
        //action执行前
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var ps=context.ActionDescriptor.Parameters;

            var arguments = context.ActionArguments;

            var method = context.HttpContext.Request.Method;

            var referer = "";

            var iflogined = false; ;

            if (context.HttpContext.Request.Headers.ContainsKey("Referer")) {

                referer = context.HttpContext.Request.Headers["Referer"];

            }

            var baseAuthClient = ServiceLocator.Instance.GetService<IBaseAuthClient>();
          
            if (arguments.Count != 0)
            {
                var model_str = JsonConvert.SerializeObject(arguments.First().Value);
                var model = JsonConvert.DeserializeObject<BaseUserInputModel>(model_str);

                if (context.HttpContext.Request.Headers.ContainsKey("AuthToken"))
                {
                    var authToken = context.HttpContext.Request.Headers["AuthToken"];
                    if (!string.IsNullOrEmpty(authToken))
                    {
                        model.AuthToken = authToken;

                        var paraObject = arguments.First().Value;
                        Type t = paraObject.GetType();
                        PropertyInfo pi1 = t.GetProperty("AuthToken");
                        //给AuthToken属性赋值 
                        pi1.SetValue(paraObject, authToken.ToString(), null);
                    }
                }

                //var model = (BaseUserInputModel)arguments.First().Value;

                var temp_r = baseAuthClient.GetUserInfo(model.AuthToken);

                if (temp_r.IsError)
                {
                    if (temp_r.Value == RS.BaseModel.Enum.EnumBaseResult.NoLogin)
                    {
                        var r = new CommonResult();
                        
                        r.Value = RS.BaseModel.Enum.EnumBaseResult.NoLogin;
                        
                        r.Message = "未登录";
                        
                        r.Result = baseAuthClient.BuildAuthUrl(referer);

                        context.Result = new ContentResult { Content = JsonConvert.SerializeObject(r), ContentType = "application/json" };

                    }
                    else { 
                    
                        context.Result = new ContentResult { Content = JsonConvert.SerializeObject(temp_r), ContentType = "application/json" };

                    }
                }
                else
                {
                    iflogined = true;
                    //补充Uid

                    var paraObject = arguments.First().Value;

                       Type t = paraObject.GetType();

                    PropertyInfo pi1 = t.GetProperty("Uid");
                    //给MyName属性赋值 
                    pi1.SetValue(paraObject, temp_r.Result.Uid, null);

                } 
                
            }
            else {
            
                var r = new CommonResult();
                r.Value = RS.BaseModel.Enum.EnumBaseResult.NoLogin;
                r.Message = "未登录";
                r.Result = baseAuthClient.BuildAuthUrl(referer);
                context.Result = new ContentResult { Content = JsonConvert.SerializeObject(r), ContentType = "application/json" };
            }

        }
  


    }
}
